package com.jamlu.framework.album;

import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;

import com.jamlu.framework.R;


/**
 * 图片选择dialog
 * Created by Kang on 2017/11/7.
 */

public class ImageSelectDialog extends DialogFragment implements View.OnClickListener {

    private Button mBtnPhotograph; // 拍照
    private Button mBtnAlbum; // 相册
    private Button mBtnCancel; // 返回
    private onSelectImageChange selectImageChange;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = new Dialog(getActivity());
        ViewGroup viewGroup = (ViewGroup) View.inflate(getActivity(), R.layout.dialog_select_image, null);
        dialog.setContentView(viewGroup);
        dialog.setCanceledOnTouchOutside(true);
        initView(dialog);
        return dialog;
    }

    private void initView(Dialog dialog){
        Window window = dialog.getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        params.gravity = Gravity.BOTTOM;
        window.setBackgroundDrawableResource(R.color.transparent);
        window.setAttributes(params);

        mBtnPhotograph = dialog.findViewById(R.id.btn_photograph);
        mBtnAlbum = dialog.findViewById(R.id.btn_album);
        mBtnCancel = dialog.findViewById(R.id.btn_cancel);

        initEvent();
    }

    private void initEvent(){
        mBtnPhotograph.setOnClickListener(this);
        mBtnAlbum.setOnClickListener(this);
        mBtnCancel.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.btn_photograph) {
            if (selectImageChange != null) {
                selectImageChange.onTakePhoto();
            }
            dismiss();
        }else if (id == R.id.btn_album) {
            if (selectImageChange != null) {
                selectImageChange.onSelectAlbum();
            }
            dismiss();
        }else {
            dismiss();
        }
    }

    public void show(FragmentManager manager, onSelectImageChange imageChange){
        if (isAdded()){
            dismiss();
        }
        this.selectImageChange = imageChange;
        super.show(manager, "ImageSelect");
    }

    public interface onSelectImageChange{

        /**
         * 选择拍照
         */
        void onTakePhoto();

        /**
         * 选择相册
         */
        void onSelectAlbum();

    }
}
